<?php
/**
 * 2020/9/30
 * Created by breeze
 * User: breeze
 */
declare(strict_types=1);
namespace app\api\business;

//use app\common\lib\sms\AliSms;
use app\common\lib\ClassAttr;
use app\common\lib\Num;
use http\Params;

class SmsBus
{
    public static function sendMessage($phone,$len,$type="ali"): bool
    {
//        $code = rand(100000, 999999);
        $code = Num::getCode($len);
        /*
         * 引入工厂思想
         */
//        $type = ucfirst($type);
//        $class = "app\common\lib\sms\\".$type."Sms";
//        $aliSms = $class::sendCode($phone,$code);
        /*
         * 工厂思想优化 利用反映射类
         * $fun = new \Reflection(A); 建立A反射类
         * $instance = $fun->newsInstanceArgs($params); 相当于实例化A对象
         */
        $classStats = ClassAttr::smsClassStat();
        $classObj = ClassAttr::initClass($type,$classStats);
        $aliSms = $classObj::sendCode($phone,$code);
        if($aliSms){
            cache(config('redis.redis_phone').$phone,$code,config("redis.redis_time"));
        }
        return $aliSms;


    }
}